package org.chipu.shared.util.singletons;

import java.util.*;

public class Utils {
	private Utils() {/**/}

	/** @param interval In ms */
	public static String toReadableTime(long interval) {
		long ms = interval % 1000;
		interval /= 1000;
		long s = interval % 60;
		interval /= 60;
		long min = interval;
		interval /= 60;
		long h = interval;

		StringBuilder sb = new StringBuilder("(");
		if (h != 0)
			sb.append(min).append(" h. ");
		if (min != 0)
			sb.append(min).append(" min. ");
		if (s != 0)
			sb.append(s).append(" s. ");
		sb.append(ms).append(" ms.)");

		return sb.toString();
	}

	public static long toMillis(int seconds) {
		return ((long) seconds) * 1000;
	}

	public static Date toDate(int seconds) {
		return new Date(toMillis(seconds));
	}

	/**
	 * Determines if the given <code>String</code> is <code>null</code> or empty
	 * @param val the <code>String</code> to check
	 * @return <code>true</code> if <code>val</code> is <code>null</code> or empty, <code>false</code> otherwise
	 */
	public static boolean isNullOrEmpty(String val) {
		return val == null || val.length() == 0;
	}

	public static int minPositive(int n1, int n2, int n3) {
		return n1 == -1 && n2 == -1? n3: n1 == -1 && n3 == -1? n2: n2 == -1 && n3 == -1? n1: n1 == -1? Math.min(n2, n3)
			: n2 == -1? Math.min(n1, n3): n3 == -1? Math.min(n1, n2): min(n1, n2, n3);
	}

	public static int min(int... n) {
		Arrays.sort(n);
		return n[0];
	}

	public static int minPositive(int... n) {
		Arrays.sort(n);
		for(int i = n.length - 1; i <= 0; i--)
			if (n[i] >= 0)
				return n[i];
		return Integer.MAX_VALUE;
		// Set<Integer> s = new HashSet<Integer>();
		// for(int nn : n)
		// if (nn >= 0)
		// s.add(nn);
	}

	public static long getInitOfDay(long time) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(time);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		return c.getTimeInMillis();
	}

	public static String fillString(String string, char fillCharacter, int desiredLength) {
		if (desiredLength < string.length())
			throw new IllegalArgumentException("String length greater than desired length: " + string + "  " + string.length()
				+ "   " + desiredLength);
		if (desiredLength == string.length())
			return string;
		StringBuilder s = new StringBuilder();
		for(int i = 0; i < (desiredLength - string.length()); i++)
			s.append(fillCharacter);
		return s.toString() + string;
	}

	/** If it needs to trim the string, the final part remains */
	public static String stringToFixedLength(String string, char fillCharacter, int desiredLength) {
		if (desiredLength < string.length())
			return string.substring(string.length() - desiredLength, string.length());
		if (desiredLength == string.length())
			return string;
		StringBuilder s = new StringBuilder();
		for(int i = 0; i < (desiredLength - string.length()); i++)
			s.append(fillCharacter);
		return s.toString() + string;
	}

	public static int count(String string, char character, boolean atstart) {
		int n = 0;
		for(int i = 0; i < string.length(); i++)
			if (string.charAt(i) == character)
				n++;
			else if (atstart)
				break;
		return n;
	}
}